import sys
import math
import pandas as pd

dir_path=sys.argv[1]

bin_df = pd.read_csv(f"{dir_path}/bins.csv")

R = (1/2) * math.log((1 + 2*0.01) / (1 - 2*0.01))

df = pd.DataFrame()

for chr_id in range(1, 11):
    chr_df = pd.read_csv(f"{dir_path}/bin_out_{chr_id}.tsv", skiprows=3, header=None)
    chr_bins = bin_df[bin_df["CHR"]==chr_id]["Bin_name"].tolist()

    bin_gms = bin_df[bin_df["CHR"]==chr_id]["Genetic_distance"].tolist()

    header_data = [[""]+[str(chr_id)]*len(chr_bins), [""]+bin_gms]
    header_df = pd.DataFrame(header_data)

    chr_df = pd.concat([header_df, chr_df])
    chr_df.columns = ["ID", *chr_bins]

    chr_df.drop("ID", inplace=True, axis=1)
    if chr_id == 1:
        df = chr_df
    else:
        df = pd.concat([df, chr_df], axis=1, ignore_index=False)

df.insert(0, " ", ["", ""]+[10]*(len(df)-2))
df.replace("b", "B", inplace=True)
df.replace("a", "A", inplace=True)
df.replace("h", "H", inplace=True)
df.to_csv(f"{dir_path}/bin_data.csv", index=False)
